﻿#region Usings

using System.Collections.Generic;
using Box2DX.Dynamics;

#endregion

namespace SilverBox.Physics
{
    public class SimpleContactListener : ContactListener
    {
        private List<ContactPoint> contactPoints = new List<ContactPoint>();

        public override void Add(ContactPoint point)
        {
            base.Add(point);

            contactPoints.Add(point);
        }

        public override void Remove(ContactPoint point)
        {
            base.Remove(point);
        }

        public void ProcessContacts()
        {
            var points = new List<ContactPoint>(contactPoints);
            contactPoints.Clear();

            foreach (var contactPoint in points)
            {
                var a = contactPoint.Shape1.UserData as Entity;
                var b = contactPoint.Shape2.UserData as Entity;

                if (a != null)
                    a.OnCollide(contactPoint);
                if (b != null)
                    b.OnCollide(contactPoint);
            }
        }
    }
}